package com.wbao.dianniu.tabstrip.fragments;


import java.util.List;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener2;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.QAInfoAdapter;
import com.wbao.dianniu.data.QaResponse;
import com.wbao.dianniu.listener.IQaListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.QaDataChangeManager;
import com.wbao.dianniu.manager.QaManager;
import com.wbao.dianniu.manager.QaDataChangeManager.IQaDataListener;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 电纽问答
 * 
 * @author yejianjun
 *
 */
public class QAFragment extends Fragment implements IQaListener,IQaDataListener{
	private final int TYPE_QA = 1;//电纽问答
	public final static int REQUEST_CODE = 10023;
	private PullToRefreshListView mListView;
	private QAInfoAdapter adapter;
	private QaManager manager;
	private ListView actualListView;
	private int currentPage = 1;
	//是否是下拉刷新
	private boolean isPullDown = false;

	public static QAFragment instance() {
		QAFragment view = new QAFragment();
		return view;
	}

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_fiset_pager_qa, null);
		init(view);
		requestData();
		return view;
	}
	
	private void init(View view){
		mListView = (PullToRefreshListView) view.findViewById(R.id.qa_listview);
		mListView.setMode(Mode.BOTH);
		mListView.setOnRefreshListener(onRefreshListener);
		
		actualListView = mListView.getRefreshableView();
		registerForContextMenu(actualListView);
		actualListView.setAdapter(adapter);
		actualListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Utils.intoQaDetailActivity(getContext(), (QaResponse)adapter.getItem(position-1));
			}
		});
	}
	
	private void requestData(){
		manager = new QaManager(getActivity());
		manager.addQaListener(this);
		reqData(currentPage);
	}
	
	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		adapter =new QAInfoAdapter(getContext());
		QaDataChangeManager.getInstance().addQaChangeListener(this);
	}
	
	@Override
	public void onQaFailure(int errorCode, String errorMsg) {
		mListView.onRefreshComplete(false,false);
	}

	@Override
	public void onQaSuccess(List<QaResponse>  list) {
		mListView.onRefreshComplete(false,false);
		//收到数据 页码加1
		if(null != list){
			if(isPullDown){
				adapter.cleanData();
			}
			adapter.addDataList(list);
			adapter.processList();
			adapter.notifyDataSetChanged();
		}
		
	}
	
	OnRefreshListener2 onRefreshListener = new OnRefreshListener2() {
		 // 下拉Pulling Down 
		@Override
		public void onPullDownToRefresh() {
			// TODO Auto-generated method stub
			//请求最新的数据
			isPullDown = true;
			currentPage = 1;
			reqData(currentPage);
		}
		@Override
		public void onPullUpToRefresh() {
			//请求下一页数据
			isPullDown = false;
			currentPage++;
			reqData(currentPage);
		}
	};
	
	private void reqData(int page){
		if(isPullDown){
			manager.reqData(GlobalContext.accountId,null,TYPE_QA,0, 10);
		}else{
			manager.reqData(GlobalContext.accountId,null,TYPE_QA,adapter.getCount(), 10);
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (null != manager) {
			manager.removeQaListener(this);
		}
		QaDataChangeManager.getInstance().removeQaChangeListener(this);
	}
	public void onResume() {
	    super.onResume();
	    MobclickAgent.onPageStart("QAFragment"); //统计页面，"MainScreen"为页面名称，可自定义
	}
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPageEnd("QAFragment"); 
	}

	@Override
	public void addOneData(QaResponse addData) {
		// TODO Auto-generated method stub
		adapter.addOneQuestData(addData);
	}

	@Override
	public void removeOneData(int questId) {
		// TODO Auto-generated method stub
		adapter.removeOneData(questId);
	}

	@Override
	public void priseNotify(int questId, int isGood, int goodCounts) {
		// TODO Auto-generated method stub
		adapter.priseNotify(questId, isGood, goodCounts);
	}

	@Override
	public void replyNotify(int questId, int answerCounts) {
		// TODO Auto-generated method stub
		adapter.replyNotify(questId, answerCounts);
	}
}
